package com.yanqu.road.entity.activity.peakstronghold;

import com.yanqu.road.entity.DataObject;
import com.yanqu.road.entity.FieldTypeAnnotation;
import com.yanqu.road.entity.union.UnionBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

// 据点战联盟实时数据
@FieldTypeAnnotation(tableName = "t_c_activity_peak_stronghold_union", autoSave = true)
public class CrossPeakStrongholdUnionData extends DataObject {
    @FieldTypeAnnotation(select = true, update = true, desc = "活动id", isIndex = true)
    private int activityId;
    @FieldTypeAnnotation(select = true, update = true, desc = "商会分组", isIndex = true)
    private int unionGroup;
    @FieldTypeAnnotation(update = true, desc = "联盟id", isIndex = true)
    private String unionId = "";
    @FieldTypeAnnotation(desc = "联盟信息")
    private UnionBaseInfo unionBaseInfo;
    @FieldTypeAnnotation(desc = "分组")
    private int group;
    @FieldTypeAnnotation(desc = "分组时排名")
    private int rank;
    @FieldTypeAnnotation(desc = "最后一次参与的回合数")
    private int lastRound;
    @FieldTypeAnnotation(desc = "是否生成了据点")
    private boolean isCreateDefend = false;
    @FieldTypeAnnotation(desc = "历史总积分")
    private BigInteger score = BigInteger.ZERO;
    @FieldTypeAnnotation(desc = "捐献经验")
    private int totalDonateExp;
    @FieldTypeAnnotation(desc = "捐献银两")
    private BigInteger totalDonateSilver = BigInteger.ZERO;
    @FieldTypeAnnotation(desc = "成员数据")
    private Map<Long, PeakStrongholdUnionMemberInfo> memberInfoMap;
    @FieldTypeAnnotation(desc = "成员防守门客,key=玩家id")
    private Map<Long, List<PeakStrongholdDefendPatrons>> defendPatrons;
    @FieldTypeAnnotation(desc = "最近一次被打时间")
    private long lastFightTime;
    @FieldTypeAnnotation(desc = "随机种子,据点列表排序用")
    private int randSeed;
    @FieldTypeAnnotation(desc = "商会总伤害")
    private BigInteger totalDamage = BigInteger.ZERO;
    @FieldTypeAnnotation(desc = "积分排行")
    private int scoreRank;
    @FieldTypeAnnotation(desc = "银两捐献排行")
    private int silverRank;
    @FieldTypeAnnotation(desc = "报名期的赚力")
    private BigInteger ability = BigInteger.ZERO;
    @FieldTypeAnnotation(desc = "成员每轮次总伤害")
    private Map<Long, List<PeakStrongholdMemberDamage>> memberDamageMap = new ConcurrentHashMap<>();
    @FieldTypeAnnotation(desc = "商会每轮次总伤害")
    private Map<Integer, PeakStrongholdUnionDamage> unionDamageMap = new ConcurrentHashMap<>();
    @FieldTypeAnnotation(desc = "商会总伤害积分")
    private BigInteger totalGroupScore = BigInteger.ZERO;

    public CrossPeakStrongholdUnionData() {
    }

    public CrossPeakStrongholdUnionData(int activityId, int unionGroup, String unionId) {
        this.activityId = activityId;
        this.unionGroup = unionGroup;
        this.unionId = unionId;
        setInsertOption();
    }

    //当前积分,临时计算，不入库
    private BigInteger nowRankScore = BigInteger.ZERO;

    public int getActivityId() {
        return activityId;
    }

    public String getUnionId() {
        return unionId;
    }


    public UnionBaseInfo getUnionBaseInfo() {
        return unionBaseInfo;
    }

    public void setActivityId(int activityId) {
        this.activityId = activityId;
    }

    public void setUnionId(String unionId) {
        if (null == unionId) {
            return;
        }
        this.unionId = unionId;
    }

    public BigInteger getScore() {
        return score;
    }

    public void setScore(BigInteger score) {
        if (this.score.compareTo(score) != 0) {
            this.score = score;
            setUpdateOption();
        }
    }

    public void setUnionBaseInfo(UnionBaseInfo unionBaseInfo) {
        this.unionBaseInfo = unionBaseInfo;
        setUpdateOption();
    }

    public void setLastFightTime(long lastFightTime) {
        if (this.lastFightTime != lastFightTime) {
            this.lastFightTime = lastFightTime;
            setUpdateOption();
        }
    }

    public int getStatus() {
        return System.currentTimeMillis() - lastFightTime < 60000 ? 1 : 0;
    }

    public long getLastFightTime() {
        return lastFightTime;
    }

    public int getRank() {
        return rank;
    }

    public void setRank(int rank) {
        if (this.rank != rank){
            this.rank = rank;
            setUpdateOption();
        }
    }

    public Map<Long, PeakStrongholdUnionMemberInfo> getMemberInfoMap() {
        return memberInfoMap;
    }

    public void setMemberInfoMap(Map<Long, PeakStrongholdUnionMemberInfo> memberInfoMap) {
        this.memberInfoMap = memberInfoMap;
    }

    public Map<Long, List<PeakStrongholdDefendPatrons>> getDefendPatrons() {
        return defendPatrons;
    }

    public List<PeakStrongholdDefendPatrons> getDefendPatronsList() {
        List<PeakStrongholdDefendPatrons> resultList = new ArrayList<>();
        for (List<PeakStrongholdDefendPatrons> patronsList : defendPatrons.values()) {
            resultList.addAll(patronsList);
        }
        return resultList;
    }

    public void setDefendPatrons(Map<Long, List<PeakStrongholdDefendPatrons>> defendPatrons) {
        this.defendPatrons = defendPatrons;
    }

    public int getGroup() {
        return group;
    }

    public void setGroup(int group) {
        this.group = group;
    }

    public boolean isCreateDefend() {
        return isCreateDefend;
    }

    public void setCreateDefend(boolean createDefend) {
        isCreateDefend = createDefend;
    }

    public int getLastRound() {
        return lastRound;
    }

    public void setLastRound(int lastRound) {
        this.lastRound = lastRound;
    }


    //获得捐献总经验
    public int updateTotalDonateExp() {
        int exp = 0;
        for (PeakStrongholdUnionMemberInfo memberInfo : memberInfoMap.values()) {
            exp += memberInfo.getDonateExp();
        }
        setTotalDonateExp(exp);

        return exp;
    }

    public PeakStrongholdUnionMemberInfo getMemberInfo(long userId) {
        PeakStrongholdUnionMemberInfo memberInfo = memberInfoMap.get(userId);
        if (null == memberInfo) {
            memberInfo = new PeakStrongholdUnionMemberInfo();
            memberInfo.setUserId(userId);
            memberInfoMap.put(userId, memberInfo);
            setUpdateOption();
        }
        return memberInfo;
    }

    //获得捐献总银两
    public BigInteger updateTotalDonateSilver() {
        BigInteger totalSilver = BigInteger.ZERO;
        for (PeakStrongholdUnionMemberInfo memberInfo : memberInfoMap.values()) {
            totalSilver = totalSilver.add(memberInfo.getSilverAmount());
        }
        setTotalDonateSilver(totalSilver);
        setUpdateOption();
        return totalSilver;
    }

    public BigInteger getTotalDonateSilver() {
        return totalDonateSilver;
    }

    public void setTotalDonateSilver(BigInteger totalDonateSilver) {
        this.totalDonateSilver = totalDonateSilver;
    }

    public void roundReset(int round) {
//        this.totalDonateSilver = BigInteger.ZERO;
        for (PeakStrongholdUnionMemberInfo memberInfo : getMemberInfoMap().values()) {
//            memberInfo.setSilverAmount(BigInteger.ZERO);
            PeakStrongholdMemberDamage memberDamage = new PeakStrongholdMemberDamage(memberInfo.getUserId(), this.group, round, memberInfo.getRoundDamage().toString());
            if(!this.getMemberDamageMap().containsKey(memberInfo.getUserId())){
                this.getMemberDamageMap().put(memberInfo.getUserId(), new ArrayList<>());
            }
            this.getMemberDamageMap().get(memberInfo.getUserId()).add(memberDamage);
            memberInfo.setRoundDamage(BigInteger.ZERO);
        }
    }

    public void setTotalDonateExp(int totalDonateExp) {
        if (this.totalDonateExp != totalDonateExp) {
            this.totalDonateExp = totalDonateExp;
            setUpdateOption();
        }
    }

    public int getTotalDonateExp() {
        return totalDonateExp;
    }

    public int getRandSeed() {
        return randSeed;
    }

    public void setRandSeed(int randSeed) {
        this.randSeed = randSeed;
    }

    public BigInteger getTotalDamage() {
        return totalDamage;
    }

    public void setTotalDamage(BigInteger totalDamage) {
        this.totalDamage = totalDamage;
    }

    public void addTotalDamage(BigInteger damage) {
        this.totalDamage = this.totalDamage.add(damage);
        setUpdateOption();
    }

    public int getScoreRank() {
        return scoreRank;
    }

    public void setScoreRank(int scoreRank) {
        if (this.scoreRank != scoreRank) {
            this.scoreRank = scoreRank;
            setUpdateOption();
        }
    }

    public int getSilverRank() {
        return silverRank;
    }

    public void setSilverRank(int silverRank) {
        if (this.silverRank != silverRank) {
            this.silverRank = silverRank;
            setUpdateOption();
        }
    }

    public BigInteger getAbility() {
        return ability;
    }

    public void setAbility(BigInteger ability) {
        this.ability = ability;
    }

    public Map<Long, List<PeakStrongholdMemberDamage>> getMemberDamageMap() {
        return memberDamageMap;
    }

    public void setMemberDamageMap(Map<Long, List<PeakStrongholdMemberDamage>> memberDamageMap) {
        this.memberDamageMap = memberDamageMap;
        setUpdateOption();
    }

    public Map<Integer, PeakStrongholdUnionDamage> getUnionDamageMap() {
        return unionDamageMap;
    }

    public void setUnionDamageMap(Map<Integer, PeakStrongholdUnionDamage> unionDamageMap) {
        this.unionDamageMap = unionDamageMap;
        setUpdateOption();
    }

    public BigInteger getNowRankScore() {
        return nowRankScore;
    }

    public void setNowRankScore(BigInteger nowRankScore) {
        this.nowRankScore = nowRankScore;
    }

    public BigInteger getTotalGroupScore() {
        return totalGroupScore;
    }

    public void setTotalGroupScore(BigInteger totalGroupScore) {
        if (totalGroupScore == null){
            return;
        }
        if (this.totalGroupScore == null || !this.totalGroupScore.equals(totalGroupScore)) {
            this.totalGroupScore = totalGroupScore;
            setUpdateOption();
        }
    }

    public int getUnionGroup() {
        return unionGroup;
    }

    public void setUnionGroup(int unionGroup) {
        this.unionGroup = unionGroup;
    }

    public String parseUnionRankParamValue(){
        List<String> paramList = new ArrayList<>();
        paramList.add(this.getScore().toString());
        paramList.add(this.getTotalGroupScore().toString());
        String paramValue = StringUtils.listToString(paramList, "=");
        return paramValue;
    }
}
